<template>
	<view class="shop-container">

		<!-- 我的控糖积分卡片 -->
		<view class="points-card">
			<view class="points-card-bg">
				<image src="https://cos.ruilongan.com/XCX/2025/08/29/Frame%201%20(2)_20250829140901A005.png"
					class="bg-image" mode="aspectFill"></image>
			</view>
			<view class="points-content">
				<view class="points-left">
					<view class="points-icon">
						<image src="https://cos.ruilongan.com/XCX/2025/08/29/积分log@2x_20250829135415A003.png"
							class="icon-image" mode="aspectFit"></image>
					</view>
					<view class="points-text">我的控糖积分</view>
				</view>
				<view class="points-value">{{usableIntegral}}</view>
			</view>
		</view>

		<!-- 搜索栏 -->
		<view class="search-bar">
			<view class="search-input-wrapper">
				<u-icon name="search" size="20"></u-icon>
				<input type="text" placeholder="搜索产品" />
			</view>
		</view>

		<!-- 分类导航 -->
		<view class="category-nav">
			<scroll-view scroll-x="true" class="scroll-view" show-scrollbar="false">
				<view class="category-item" v-for="(item, index) in categories" :key="index"
					:class="{ active: currentCategory === index }" @tap="selectCategory(index)">
					{{ item.name }}
				</view>
			</scroll-view>
		</view>

		<!-- 商品列表 -->
		<scroller @init="initScroller" @down="refreshData" @up="queryGoodsList" :up="optUp" :fixed="false">
			<view class="product-grid">
				<view class="product-item" v-for="(product, index) in products" :key="index"
					@tap="goGoodDetail(product)">
					<img :src="product.image | avatarUrlFind('1')" :alt="product.drugName" />
					<view class="product-info">
						<view class="product-title">{{ product.drugName }}</view>
						<view class="product-specs">{{ product.productInfo }}</view>
						<view class="price-sales-row">
							<view class="price-info">
								<view class="product-price">
									<image
										src="https://cos.ruilongan.com/XCX/2025/08/29/积分log@2x_20250829135415A003.png"
										class="price-icon" mode="aspectFit"></image>
									{{ product.salesPrice }}
								</view>
								<view class="market-price">市场价 ¥{{ product.dashingPrice}}</view>
							</view>

							<view class="exchange-btn" @tap.stop="exchangeProduct(product)">立刻兑换</view>
						</view>
					</view>
				</view>
			</view>
		</scroller>

	</view>
</template>

<script>
	import scroller from '@/components/scroller/scroller.vue';
	import {
		queryGoodsList,
		queryGoodsTypeList
	} from '@/api/shop.js'
	import {
		getInfo
	} from '@/api/vipUser.js'
	import {
		mapGetters
	} from "vuex"

	export default {
		computed: mapGetters(['name', 'userId']),
		head: {
			link: [{
				rel: 'stylesheet',
				href: 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css'
			}]
		},
		data() {
			return {
				currentCategory: 0,
				scroller: {},
				optUp: {
					onScroll: true, //监听滚动事件
					page: {
						size: 5
					} //下拉加载15条
				},
				queryParams: {},
				categories: [],
				products: [],
				usableIntegral: undefined
			}
		},
		created() {
			this.queryGoodsTypeList()
		},
		onShow() {
			this.getInfo()
		},
		methods: {

			/*初始化滚动*/
			initScroller(scroller) {
				this.scroller = scroller;
			},
			/*刷新数据*/
			refreshData() {
				this.loadData()
			},
			/*加载数据*/
			loadData() {
				this.products = [];
				this.scroller.resetUpScroll();
			},


			selectCategory(index) {
				this.currentCategory = index;
				this.drugType = this.categories[index].id
				this.loadData()
			},
			goGoodDetail(product) {
				uni.navigateTo({
					url: '/subPackage_1/pages/goods/good-detail?id=' + product.id
				});
			},
			queryGoodsTypeList() {
				queryGoodsTypeList({
					parentId: '100'
				}).then(res => {
					console.log('------商品类型------', res.rows)
					this.categories = res.rows
				})
			},
			async queryGoodsList() {
				this.queryParams.pageNum = this.scroller.num
				this.queryParams.pageSize = this.scroller.size
				this.queryParams.isShow = '1'
				this.queryParams.drugType = this.drugType
				this.queryParams.shopFlag = 'points'
				const {
					rows,
					total
				} = await queryGoodsList(this.queryParams)
				this.products = this.products.concat(rows);
				this.$forceUpdate();
				this.scroller.endBySize(rows.length, total);
				uni.stopPullDownRefresh();
				uni.hideLoading();
			},

			getInfo() {
				if (this.userId) {
					getInfo(this.userId).then(res => {
						this.usableIntegral = res.data.usableIntegral
					})
				}
			},

			exchangeProduct(product) {
				// 检查积分是否足够
				if (this.usableIntegral < product.salesPrice) {
					uni.showToast({
						title: '积分不足',
						icon: 'none'
					});
					return;
				}

				// 跳转到兑换确认页面或直接兑换
				uni.navigateTo({
					url: '/subPackage_1/pages/goods/good-detail?id=' + product.id + '&exchange=true'
				});
			}

		}
	}
</script>

<style scoped>
	.shop-container {
		padding: 12px;
		background-color: #fff;
	}

	/* 积分卡片样式 */
	.points-card {
		position: relative;
		margin-bottom: 20px;
		border-radius: 12px;
		overflow: hidden;
		height: 130px;
	}

	.points-card-bg {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 1;
	}

	.bg-image {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}

	.points-content {
		position: relative;
		z-index: 2;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20px;
		height: 100%;
		/* background: linear-gradient(135deg, rgba(0,0,0,0.7) 0%, rgba(0,0,0,0.5) 100%); */
	}

	.points-left {
		display: flex;
		align-items: center;
	}

	.points-icon {
		margin-right: 12px;
		/* background: rgba(255, 255, 255, 0.1); */
		border-radius: 50%;
		width: 40px;
		height: 40px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.icon-image {
		width: 24px;
		height: 24px;
	}

	.points-text {
		color: #FFDBB5;
		font-size: 16px;
		/* font-weight: 500; */
	}

	.points-value {
		color: #FFDBB5;
		font-size: 24px;
		/* font-weight: bold; */
	}

	.title {
		font-size: 20px;
		font-weight: bold;
	}

	.more-icon {
		font-size: 20px;
		transform: rotate(90deg);
	}

	.search-bar {
		margin-bottom: 15px;
		/* padding: 10px 15px; */
	}

	.search-input-wrapper {
		position: relative;
		display: flex;
		align-items: center;
		background-color: #f1f1f1;
		border-radius: 5px;
		padding: 8px 15px;
	}

	.search-icon {
		font-size: 16px;
		color: #999;
		margin-right: 8px;
		width: 16px;
		height: 16px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.search-bar input {
		width: 100%;
		border: none;
		background: transparent;
		font-size: 14px;
		color: #333;
		outline: none;
	}

	.search-bar input::placeholder {
		color: #999;
	}

	.category-nav {
		margin-bottom: 20px;
		width: 100%;
	}

	.scroll-view {
		white-space: nowrap;
		width: 100%;
	}

	.category-item {
		display: inline-block;
		padding: 8px 16px;
		font-size: 14px;
		color: #666;
		position: relative;
	}

	.category-item.active {
		color: #333;
		font-weight: bold;
	}

	.category-item.active::after {
		content: '';
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
		width: 20px;
		height: 3px;
		background-color: #7ad1a0;
		border-radius: 100px 100px 0 0;
		box-shadow: none;
	}

	.product-grid {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 15px;
	}

	.product-item {
		background: #fff;
		border-radius: 4px;
		overflow: hidden;
	}

	.product-item img {
		width: 100%;
		/* height: 200px; */
		object-fit: cover;
	}

	.product-info {
		padding: 10px;
	}

	.product-title {
		font-size: 14px;
		margin-bottom: 5px;
		color: #333;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}

	.product-specs {
		font-size: 12px;
		color: #999;
		margin: 4px 0 8px 0;
	}

	.price-sales-row {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.price-info {
		display: flex;
		flex-direction: row;
		align-items: center;
		gap: 8px;
	}

	.product-price {
		color: #ff4d4f;
		font-size: 16px;
		font-weight: bold;
		display: flex;
		align-items: center;
	}

	.price-icon {
		width: 16px;
		height: 16px;
		margin-right: 4px;
	}

	.market-price {
		font-size: 10px;
		color: #999;
		text-decoration: line-through;
	}

	.exchange-btn {
		background: linear-gradient(135deg, #FFC608, #FC3F06);
		color: #fff;
		font-size: 10px;
		padding: 4px 6px;
		border-radius: 12px;
		border: none;
		cursor: pointer;
		transition: all 0.3s ease;
		box-shadow: 0 2px 8px rgba(255, 198, 8, 0.3);
	}

	.exchange-btn:active {
		transform: scale(0.95);
		box-shadow: 0 1px 4px rgba(122, 209, 160, 0.3);
	}
</style>